package com.freesky.provider.content.service.impl;

import com.freesky.common.dao.entity.HomeRotationChart;
import com.freesky.common.dao.mapper.HomeRotationChartMapper;
import com.freesky.common.enums.ResponseEnum;
import com.freesky.common.enums.StateEnum;
import com.freesky.common.exception.MyException;
import com.freesky.common.utils.TimeUtil;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.provider.content.service.IHomeRotationChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HomeRotationChartServiceImpl implements IHomeRotationChartService {
    @Autowired
    private HomeRotationChartMapper homeRotationChartMapper;
    @Override
    public JSONResponse delete(int[] ids) {
        for (int id : ids) {
            HomeRotationChart homeRotationChart = new HomeRotationChart(id);
            try {
                homeRotationChart.deleteById();
            } catch (Exception e) {
                throw new MyException(ResponseEnum.DELETE_HOME_ROTATION_CHART_ERROR, e, null);
            }

        }
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse portalList() {
        List<HomeRotationChart> list = homeRotationChartMapper.getAll(StateEnum.INUSE);
        return JSONResponseDiretor.buildSuccessJSONResponse(list);
    }

    @Override
    public JSONResponse add(HomeRotationChart rotationChart) {
        String currentTime = TimeUtil.getDateTimeNow();
        rotationChart.setCreateTime(currentTime);
        rotationChart.setUpdateTime(currentTime);
        try {
            rotationChart.insert();
            return JSONResponseDiretor.buildSuccessJSONResponse(null);
        } catch (Exception e) {
            throw new MyException(ResponseEnum.ADD_HOME_ROTATION_CHART_ERROR, e, null);
        }
    }

    @Override
    public JSONResponse update(HomeRotationChart rotationChart) {
        rotationChart.setUpdateTime(TimeUtil.getDateTimeNow());
        try {
            rotationChart.updateById();
            return JSONResponseDiretor.buildSuccessJSONResponse(null);
        } catch (Exception e) {
            throw new MyException(ResponseEnum.UPDATE_HOME_ROTATION_CHART_ERROR, e, null);
        }
    }
}
